(ns ura.migrations.testData
  (:require
   [toucan2.core :as t2]
   [methodical.core :as m]
   [taoensso.telemere :as t]
   [ura.config :as cfg]
   )
  )


(m/defmethod t2/do-with-connection :default
  [_connectable f]
  (t2/do-with-connection cfg/dm f))

(defn migrate-up [config]
  (t/log! "insert test data" )
  (t2/insert! :models/USER_INFO
              [:ID, :USERNAME, :CODE, :PASSWORD, :STATE, :INFOS, :SEX :AGE, :COMMENT]
              [
               [1, "超级管理员", "admin",       "123456", "enable", "", "", 0, ""]
               [2, "数据管理员", "dataManager", "123456", "enable", "", "", 0, ""]
               [3, "任务管理员", "taskManager", "123456", "enable", "", "", 0, ""]
               [4, "排班管理员", "arrangeManager", "123456", "enable", "", "", 0, ""]
               [5, "游客", "guest", "guest", "enable", "", "", 0, ""]

               [11, "admin1", "admin1", "admin1", "enable", "", "", 0, ""]
               [12, "admin2", "admin2", "admin2", "enable", "", "", 0, ""]
               [13, "admin3", "admin3", "admin3", "enable", "", "", 0, ""]
               [14, "admin4", "admin4", "admin4", "enable", "", "", 0, ""]
               [15, "admin5", "admin5", "admin5", "enable", "", "", 0, ""]
               [16, "admin6", "admin6", "admin6", "enable", "", "", 0, ""]
               [17, "admin7", "admin7", "admin7", "enable", "", "", 0, ""]
               [18, "admin8", "admin8", "admin8", "enable", "", "", 0, ""]
               [19, "admin9", "admin9", "admin9", "enable", "", "", 0, ""]
               ])

  (doall (map
          (fn [i]
            (t2/insert! :models/USER_INFO
                        [:ID, :USERNAME, :CODE, :PASSWORD, :STATE, :INFOS, :SEX :AGE, :COMMENT]
                        [
                         [i, (str "admin" i), (str "admin" i), "1234567890", "enable", "", "", 0, ""]
                         ])
            )
            (range 100 300)
          ))

  (t2/insert! :models/ORG_INFO
              [:ID :NAME :CODE :DESCRIPTION  :STATE :PARENT_ID :INFOS]
              [
               [1, "com", "com", "desc1 asd", "enable", 1, "{\"tel\": \"12345678\"}"]
               [2, "dept1", "dept1", "desc2", "enable", 1, "{\"tel\": \"87654321\"}"]
               ])
  
  (t2/insert! :models/AUTH_INFO
              [:ID :NAME :CATEGORY :CODE :DESCRIPTION  :STATE]
              [
               [1, "任务管理权限",     "zdrw", "taskManager", "", "enable"]
               [2, "修改任务历史数据", "zdrw", "modifyTaskHistory", "", "enable"]
               [3, "设备管理权限",     "zdrw", "deviceManager", "", "enable"]
               [4, "用户管理权限",     "zdrw", "userManager", "", "enable"]
               [5, "账号管理权限",     "zdrw", "accountManager", "", "enable"]
               [6, "跳转图谱权限",     "zdrw", "menulink", "", "enable"]

               [11, "用户管理权限",    "ura",  "user", "", "enable"]
               [12, "组织管理权限",    "ura",  "org", "", "enable"]
               [13, "角色管理权限",    "ura",  "role", "", "enable"]
               [14, "权限管理权限",    "ura",  "auth", "", "enable"]
               [15, "日志管理权限",    "ura",  "log", "", "enable"]

               [21, "超级管理权限",    "zb",  "superAdmin", "", "enable"]
               [22, "数据管理权限",    "zb",  "databaseAdmin", "", "enable"]
               [23, "排班管理权限",    "zb",  "arrangeAdmin", "", "enable"]
               ])
  (t2/insert! :models/ROLE_INFO
              [:ID :NAME :CODE :DESCRIPTION :STATE]
              [
               [1, "超级管理角色", "admin", "" "enable"]
               [2, "数据管理角色", "dataManager", "" "enable"]
               [3, "任务管理角色", "taskManager", "" "enable"]
               [4, "排班管理角色", "arrangeAdmin", "" "enable"]
               [5, "游客角色",     "guest", "" "enable"]
               ]
              )
  (t2/insert! :models/ROLE_TO_AUTH
              [:ROLE_ID :AUTH_ID]
              [
               [1, 1]
               [1, 2]
               [1, 3]
               [1, 4]
               [1, 5]
               [1, 6]
               
               [1, 11]
               [1, 12]
               [1, 13]
               [1, 14]
               [1, 15]

               [1, 21]

               [2, 1]
               [2, 3]
               [2, 4]
               [2, 5]
               [2, 6]

               [2, 15]
               [2, 22]
               
               [3, 1]
               [4, 23]
               ]
              )
  (t2/insert! :models/USER_TO_ROLE
              [:USER_ID :ROLE_ID]
              [
               [1, 1]
               [2, 2]
               [3, 3]
               [4, 4]
               [5, 5]

               [11, 2]
               [12, 2]
               [13, 2]
               [14, 2]
               [15, 2]
               [16, 2]
               [17, 2]
               [18, 2]
               [19, 2]
               ]
              )
  
  )

(defn migrate-down [config]
  (println "start delete test data" )
  (t2/delete! :models/USER_TO_ORG)
  (t2/delete! :models/USER_TO_ORG)
  (println "deleted user_to_org data" )
  (t2/delete! :models/ROLE_TO_AUTH)
  (t2/delete! :models/ROLE_TO_AUTH)
  (println "deleted role_to_auth data" )
  (t2/delete! :models/USER_TO_ROLE)
  (t2/delete! :models/USER_TO_ROLE)
  (println "deleted user_to_role data" )
  (t2/delete! :models/USER_INFO)
  (t2/delete! :models/USER_INFO)
  (println "deleted user_info data" )
  (t2/delete! :models/ORG_INFO)
  (t2/delete! :models/ORG_INFO)
  (println "deleted org_info data" )
  (t2/delete! :models/AUTH_INFO)
  (t2/delete! :models/AUTH_INFO)
  (println "deleted auth_info data" )
  (t2/delete! :models/ROLE_INFO)
  (t2/delete! :models/ROLE_INFO)
  (println "deleted role data" )
  (t2/delete! :models/LOG_DATA)
  (t2/delete! :models/LOG_DATA)
  "deleted test data"
  )
